var express=require("express");
var cookieParser = require('cookie-parser')
var app =express();
app.use(cookieParser())
app.param('user', function(req, res, next, id) {

  // try to get the user details from the User model and attach it to the request object
  User.find(id, function(err, user) {
    if (err) {
      next(err);
    } else if (user) {
      req.user = user;
      next();
    } else {
      next(new Error('failed to load user'));
    }
  });
});
//会根据路由匹配出id
app.param('id', function (req, res, next, id) {
  console.log('CALLED ONLY ONCE');
  next();
});
app.get("/",function (req,res) {
    
    res.send("cookie:"+req.cookies);
    res.send("test express");
});
// app.get('/user/:id', function (req, res, next) {
//   console.log('although this matches');
//   next();
// });
//param匹配的路由
app.get('/user/:id', function (req, res) {
  console.log('and this matches too');
  res.end();
});

app.get(/(\d+)\/[-_\w\d]+?\/[\w\d_-]+?\//g,function (req,res) {
   res.send("test regex");
});
var server=app.listen("8081",function(){
    var host=server.address().address;
    var port=server.address().port;
    console.log("应用实例，访问地址为 http://%s:%s", host, port);
})